package com.example.service.impl;

import com.example.mapper.EmpMapper;
import com.example.pojo.Emp;
import com.example.service.EmpService;
import com.example.util.Md5Util;
import com.example.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public void register(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
        empMapper.register(username,md5String);
    }

    @Override
    public Emp findByUsername(String username) {
        Emp emp = empMapper.findByUsername(username);
        return emp;
    }

    @Override
    public void updateEmpInfo(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmpInfo(emp);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        empMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String rePwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String md5String = Md5Util.getMD5String(rePwd);
        empMapper.updatePwd(md5String,id);
    }
}
